package me.idashu.attg.controller;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import me.idashu.attg.domain.AreaEntity;
import me.idashu.attg.service.AreaService;
import me.idashu.attg.util.Escape;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/area")
public class AreaController {
	 @Autowired
	 private AreaService areaService;
	 @RequestMapping("/addArea")
	 public String addArea(Model model,HttpServletRequest request){
		 try{
			 model.addAttribute("addArea", "添加地区");
		 }catch(Exception ex){
			 ex.printStackTrace();
			 return "error";
		 }
		 return "add_areacode";
	 }
	 
	 @RequestMapping("/addAreaInfo")
	 public String addAreaInfo(@ModelAttribute AreaEntity areaEntity,Model model,HttpServletRequest request){
		 try{
//			 areaEntity.setAreaname(Escape.unescape(areaEntity.getAreaname()));
			 Random ran = new Random();
			 String areacode =""+ran.nextInt(9)+ran.nextInt(9)+ran.nextInt(9)+ran.nextInt(9);
			 areaEntity.setAreacode(areacode);
			 areaService.insertArea(areaEntity);
			 model.addAttribute("success", "添加地区成功");
		 }catch(Exception ex){
			 ex.printStackTrace();
			 return "error";
		 }
		 return "success";
	 }
}
